<template>
  <div>
    <!--     搜索条件-->
    <SearchForm @search="getTableData" @reset="clearSearch">
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('订单编号')" prop="loanRecordId">
          <el-input v-model="page.loanRecordId"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('手机号码')" prop="phone">
          <el-input v-model="page.phone"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="8" :xs="24" v-show="$permiss[532]">
        <el-form-item :label="$t('订单状态')" prop="approvalResult">
          <el-select style="width: 100%" v-model="page.approvalResult" clearable>
            <el-option v-for="item in userStatusList" :key="item.key" :label="item.value"
                       :value="item.key"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[533]">
        <el-form-item :label="$t('APP名称')" prop="productName">
          <el-select style="width: 100%" v-model="page.productName" clearable>
            <el-option v-for="item in productList" :key="item.productId" :value="item.productId"
                       :label="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('用户状态')" prop="isRepeatedBorrowing">
          <el-select style="width: 100%" v-model="page.isRepeatedBorrowing" clearable>
            <el-option :label="$t('复借')" :value="1"></el-option>
            <el-option :label="$t('初借')" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('机审状态')" prop="machine">
          <el-select style="width: 100%" v-model="page.machine" clearable>
            <el-option :label="$t('通过')" :value="0"></el-option>
            <el-option :label="$t('拒绝')" :value="1"></el-option>
            <el-option :label="$t('审核中')" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('审核状态')" prop="auditStatus">
          <el-select style="width: 100%" v-model="page.auditStatus" clearable>
            <el-option v-for="item in auditStatusList" :key="item.value" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('申请时间')" prop="applyTimeValue">
          <el-date-picker
              v-model="page.applyTimeValue"
              style="width: 100%"
              type="daterange"
              unlink-panels
              range-separator="~"
              value-format="yyyy-MM-dd"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('审核时间')" prop="examineTimeValue">
          <el-date-picker
              v-model="page.examineTimeValue"
              style="width: 100%"
              unlink-panels
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="~"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </SearchForm>

    <el-card style="margin-top: 10px" shadow="never">
      <el-row>
        <Table :table-data="tableData" row-key="id" :page="page" :table-column="tableColumn"
               @handleSizeChange="handleSizeChange"
               :loading="loading"
               @handleCurrentChange="handleCurrentChange">
          <!--          <template slot="first">-->
          <!--            <el-table-column :label="this.$t('订单编号')" prop="loanRecordId" align="center" width="200">-->
          <!--              <template slot-scope="scope">-->
          <!--                <el-link :underline="false" v-if="$permiss[88]" type="primary"-->
          <!--                         @click="goOrderDetails(scope.row)">{{-->
          <!--                    scope.row.loanRecordId-->
          <!--                  }}-->
          <!--                </el-link>-->
          <!--                <span v-else>{{ scope.row.loanRecordId }}</span>-->
          <!--              </template>-->
          <!--            </el-table-column>-->
          <!--          </template>-->
        </Table>
      </el-row>
    </el-card>
  </div>
</template>

<script>

import {fetchHandledOrder} from "@/api/order";
import {selectProduct} from "@/api/system";
import {dateFormatSection} from "@/utils/tools";

export default {
  name: 'orderHistory',
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      userStatusList: [
        {key: -1, value: this.$t('已关闭'), color: '#5e5e5e'},
        {key: 0, value: this.$t("未分配"), color: '#1200ff'},
        {key: 1, value: this.$t("审核中"), color: "#5e5e5e"},
        {key: 2, value: this.$t("拒绝"), color: "#9c0000"},
        {key: 5, value: this.$t("待打款"), color: "#009200"},
        {key: 6, value: this.$t("待还款"), color: "#ffc000"},
        {key: 8, value: this.$t("逾期"), color: "#ff0000"},
        {key: 9, value: this.$t("结清"), color: "#5e5e5e"},
      ],
      productList: [],
      tableColumn: [
        {label: this.$t('订单编号'), prop: 'loanRecordId', align: 'center', minWidth: 200, fixed: 'left'},
        {label: this.$t('用户姓名'), prop: 'name', align: 'center', minWidth: 150},
        {label: this.$t('手机号码'), prop: 'phone', align: 'center', minWidth: 100},
        {label: this.$t('白名单'), prop: 'isWhite', align: 'center'},
        {label: this.$t('申请时间'), prop: 'loanTime', align: 'center', minWidth: 150},
        {label: this.$t('申请金额'), prop: 'loanAmount', align: 'center', minWidth: 150},
        {label: this.$t('借款周期'), prop: 'loanTerm', align: 'center'},
        {label: this.$t('机审状态'), prop: 'machine', align: 'center', isHtml: true},
        {label: this.$t('审核状态'), prop: 'auditStatus', align: 'center', minWidth: 100},
        {label: this.$t('订单状态'), prop: 'approvalResult', align: 'center', component: 'OrderStatus'},
        {label: this.$t('黑猫分'), prop: 'score', align: 'center', minWidth: 100},
        {label: this.$t('审核时间'), prop: 'approvalTime', align: 'center', minWidth: 150},
        {label: this.$t('APP名称'), prop: 'appName', align: 'center', minWidth: 150},
      ],
      tableData: [],
      auditStatusList: [
        {label: this.$t('审核中'), value: 0},
        {label: this.$t('拒绝'), value: 1},
        {label: this.$t('通过'), value: 2},
        {label: this.$t('关闭'), value: 3},
      ],
      loading: false
    }
  },
  mounted() {
    this.getTableData()
    this.getAppList()
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    // 获取产品列表
    async getAppList() {
      this.productList = await selectProduct().then(res => res.data)
    },
    clearSearch() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      }
      this.getTableData()
    },
    async getTableData() {
      const [loanDatetimeStart, loanDatetimeEnd] = dateFormatSection(this.page.applyTimeValue)
      const [datetimeStart, datetimeEnd] = dateFormatSection(this.page.examineTimeValue)
      const params = {
        ...this.page,
        datetimeStart,
        datetimeEnd,
        loanDatetimeStart,
        loanDatetimeEnd,
      }
      // 删除日期属性
      delete params.examineTimeValue
      delete params.applyTimeValue
      this.loading = true
      const res = await fetchHandledOrder(params)
      if (res.code === 1) {
        const {list,total} = res.data
        const machineStatus = (value, color) => {
          return `<span class="highlight" style="color: ${color}">${value}</span>`
        }
        this.tableData = list.map(item => {
          const statusObj = {
            0: this.$t('审核中'),
            1: this.$t('拒绝'),
            2: this.$t('通过'),
            3: this.$t('关闭')
          }
          item.machine = item.machine === 0 ? machineStatus(this.$t('通过'), '#4ce308') : item.machine === 1 ? machineStatus(this.$t('拒绝'), '#f51458') : machineStatus(this.$t('审核中'))
          item.auditStatus = statusObj[item.auditStatus] ? statusObj[item.auditStatus] : ''
          return item
        })
        this.page.total = total
        this.loading = false
      }
    },
    goOrderDetails(row) {
      // 埋点当前搜索参数
      this.$router.push({
        path: '/order/orderDetails',
        query: {
          userStubId: row.userStubId,
          userId: row.userId,
          approvalId: row.approvalId,
          orderId: row.loanRecordId,
          permissId: 88
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
